package com.lxmes.hr.service.impl;

import java.util.Date;
import java.util.List;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.hr.domain.NcLeaveApproval;
import com.lxmes.hr.domain.NcLeaveApprovalRules;
import com.lxmes.hr.domain.NcOrgManage;
import com.lxmes.hr.mapper.NcLeaveApprovalMapper;
import com.lxmes.hr.mapper.NcLeaveApprovalRulesMapper;
import com.lxmes.hr.mapper.NcOrgManageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.hr.mapper.NcLeaveFormMapper;
import com.lxmes.hr.domain.NcLeaveForm;
import com.lxmes.hr.service.INcLeaveFormService;
import javax.validation.Validator;

/**
 * 请假单Service业务层处理
 *
 * @author lx
 * @date 2023-04-11
 */
@Service
public class NcLeaveFormServiceImpl implements INcLeaveFormService
{
    @Autowired
    private NcLeaveFormMapper ncLeaveFormMapper;

    @Autowired
    private NcLeaveApprovalMapper ncLeaveApprovalMapper;


    @Autowired
    private NcLeaveApprovalRulesMapper ncLeaveApprovalRulesMapper;

    @Autowired
    private NcOrgManageMapper ncOrgManageMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询请假单
     *
     * @param id 请假单主键
     * @return 请假单
     */
    @Override
    public NcLeaveForm selectNcLeaveFormById(Long id)
    {
        return ncLeaveFormMapper.selectNcLeaveFormById(id);
    }

    /**
     * 查询请假单列表
     *
     * @param ncLeaveForm 请假单
     * @return 请假单
     */
    @Override
    public List<NcLeaveForm> selectNcLeaveFormList(NcLeaveForm ncLeaveForm)
    {
        return ncLeaveFormMapper.selectNcLeaveFormList(ncLeaveForm);
    }

    /**
     * 新增请假单
     *
     * @param ncLeaveForm 请假单
     * @return 结果
     */
    @Override
    public int insertNcLeaveForm(NcLeaveForm ncLeaveForm)
    {
        List<NcOrgManage> ncOrgManages = ncOrgManageMapper.selectNcOrgManageList(null);
        if(!StringUtils.isNull(ncOrgManages)){
            ncLeaveForm.setGcCode(ncOrgManages.get(0).getJtdm());
            ncLeaveForm.setGcName(ncOrgManages.get(0).getJtmc());
        }
        ncLeaveForm.setCreateTime(DateUtils.getNowDate());
        ncLeaveForm.setCreateBy(SecurityUtils.getUsername());
        return ncLeaveFormMapper.insertNcLeaveForm(ncLeaveForm);
    }

    /**
     * 修改请假单
     *
     * @param ncLeaveForm 请假单
     * @return 结果
     */
    @Override
    public int updateNcLeaveForm(NcLeaveForm ncLeaveForm)
    {
        ncLeaveForm.setUpdateTime(DateUtils.getNowDate());
        return ncLeaveFormMapper.updateNcLeaveForm(ncLeaveForm);
    }

    /**
     * 批量删除请假单
     *
     * @param ids 需要删除的请假单主键
     * @return 结果
     */
    @Override
    public int deleteNcLeaveFormByIds(Long[] ids)
    {
        for(long id : ids){
            NcLeaveForm ncLeaveForm = ncLeaveFormMapper.selectNcLeaveFormById(id);
            if("1".equals(ncLeaveForm.getState())){
                throw new RuntimeException("请假单已提交不能删除！");
            }
        }
        return ncLeaveFormMapper.deleteNcLeaveFormByIds(ids);
    }

    /**
     * 删除请假单信息
     *
     * @param id 请假单主键
     * @return 结果
     */
    @Override
    public int deleteNcLeaveFormById(Long id)
    {
        NcLeaveForm ncLeaveForm = ncLeaveFormMapper.selectNcLeaveFormById(id);
        if("1".equals(ncLeaveForm.getState())){
            throw new RuntimeException("请假单已提交不能删除！");
        }
        return ncLeaveFormMapper.deleteNcLeaveFormById(id);
    }
    @Override
    public String imporIteminfo(List<NcLeaveForm> ncLeaveFormList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ncLeaveFormList) || ncLeaveFormList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcLeaveForm ncLeaveForm : ncLeaveFormList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ncLeaveForm);
                ncLeaveForm.setCreateBy(operName);
                ncLeaveForm.setUpdateBy(operName);
                this.insertNcLeaveForm(ncLeaveForm);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public int handleTjsq(Long id) {
        NcLeaveForm ncLeaveForm = ncLeaveFormMapper.selectNcLeaveFormById(id);
        NcLeaveApprovalRules rule = new NcLeaveApprovalRules();
        rule.setJobNumber(ncLeaveForm.getJobNumber());
        List<NcLeaveApprovalRules> ncLeaveApprovalRulesList = ncLeaveApprovalRulesMapper.selectNcLeaveApprovalRulesList(rule);
        if(ncLeaveApprovalRulesList.size()==0){
            throw new RuntimeException("请先创建该员工的审批规则流程！");
        }

        NcLeaveApproval ncLeaveApproval = new NcLeaveApproval();
        BeanUtils.copyProperties(ncLeaveForm,ncLeaveApproval);
        ncLeaveApproval.setJbr(ncLeaveForm.getCreateBy());
        NcLeaveApprovalRules ncLeaveApprovalRules = ncLeaveApprovalRulesList.get(0);
        while (true){
            if(ncLeaveApprovalRules.isBzzHd()){
                ncLeaveApproval.setNextUser("bzz");
                ncLeaveApproval.setNextRoleName("bzz");
                break;
            }


            if(ncLeaveApprovalRules.isBmjlHd()){
                ncLeaveApproval.setNextUser("bmjl");
                ncLeaveApproval.setNextRoleName("bmjl");
                break;
            }

            if(ncLeaveApprovalRules.isDszHd()){
                ncLeaveApproval.setNextUser("dsz");
                ncLeaveApproval.setNextRoleName("dsz");
                break;
            }
        }


        ncLeaveApprovalMapper.insertNcLeaveApproval(ncLeaveApproval);
        ncLeaveForm.setState("1");
        ncLeaveForm.setUpdateBy(SecurityUtils.getUsername());
        ncLeaveForm.setUpdateTime(new Date());
        ncLeaveFormMapper.updateNcLeaveForm(ncLeaveForm);
        return 1;
    }
}
